<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>03.传递get请求参数</title>
  </head>
  <body>
    <p>
      姓名:
      <input type="text" name="username" id="username" />
    </p>
    <p>
      年龄:
      <input type="text" name="age" id="age" />
    </p>
    <p>
      <input type="button" value="提交" id="commit" />
    </p>
  </body>
</html>
<script>
  let btn = document.getElementById("commit");
  let username = document.getElementById("username");
  let age = document.getElementById("age");
  btn.onclick = function () {
    let nameValue = username.value;
    let ageValue = age.value;
    console.log(nameValue);
    console.log(ageValue);
    /*
     *     ajax 异步提交
     */
    // 1.创建ajax 对象
    const xhr = new XMLHttpRequest();
    //获取表单提交的数据
    let params = "username=" + nameValue + "&age=" + ageValue;
    // 2. 配置ajax对象  增加传递的参数    get方式传递
    xhr.open("get", "http://localhost:3000/get?" + params);
    // 3. 发送请求
    xhr.send();
    // 4. 对响应的数据做处理
    xhr.onload = function () {
      // 响应的数据
      console.log(xhr.responseText);
    };
  };
</script>
